<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mysms{
    private $_apikey = "51460d0b8d8d6b64c4ee4f8c63adb68f";

    public function send_sms($text, $mobile){
        $url="https://sms.yunpian.com/v2/sms/single_send.json";
        $data = array('text'=>$text,'apikey'=>$this->_apikey,'mobile'=>$mobile);
        return $this->http_request($url, $data);
    }

    public function tpl_send_sms($tpl_id, $tpl_value, $mobile){
    	//$tpl_value 调用前要转义，tplvalue=urlencode("#code#") + "=" + urlencode("1234") + "&" + urlencode("#company#") + "=" + urlencode("云片网"); 
        $url="https://sms.yunpian.com/v2/sms/tpl_single_send.json";
        $data = array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$this->_apikey,'mobile'=>$mobile);
        return $this->http_request($url, $data);
    }
    
    public function http_request($url,$data)
    {
    	header("Content-Type:text/html;charset=utf-8");
    	$ch = curl_init();
    	
    	/* 设置验证方式 */    	
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
    	
    	/* 设置返回结果为流 */
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	
    	/* 设置超时时间*/
    	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    	
    	/* 设置通信方式 */
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	
    	curl_setopt ($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    	
    	$res = curl_exec($ch);
    	curl_close($ch);
    	
    	return json_decode($res,true);
    }
}